package org.upfrost.converter;

public class DateTimeConverters implements Converter {

	@Override
	public Object convert(Object from, Class as) 
	{
		if (from instanceof java.util.Date && as.equals(java.sql.Timestamp.class))
		{
			return new java.sql.Timestamp(((java.util.Date)from).getTime());
		}
		else if (from instanceof java.sql.Timestamp && as.equals(java.util.Date.class))
		{
			return from;
		}
		
		return null;
	}

	@Override
	public boolean handles(Class from, Class as) {
		
		if (matchClass(from, java.util.Date.class, java.sql.Timestamp.class)
				&& matchClass(as, java.util.Date.class, java.sql.Timestamp.class))
			return true;
		
		return false;
	}

	static public boolean matchClass(Class c, Class...classes)
	{
		for (Class cc: classes)
		{
			if (cc.equals(cc))
				return true;
			
			if (cc.isAssignableFrom(c))
				return true;
		}
		return false;
	}
	
}
